﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace Hp.Vertica.Client
{
    public class BooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Boolean?) && targetType != typeof(Boolean))
            {
                throw new InvalidOperationException("The target must be Nullable<Boolean> or Boolean");
            }
            return value == null ? false : Boolean.Parse(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Boolean?) && targetType != typeof(Boolean))
            {
                throw new InvalidOperationException("The target must be Nullable<Boolean> or Boolean");
            }
            return value == null ? false : Boolean.Parse(value.ToString());
        }
    }
    public class BooleanReverserConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Boolean?) && targetType != typeof(Boolean))
            {
                throw new InvalidOperationException("The target must be Nullable<Boolean> or Boolean");
            }
            return value == null ? true : !Boolean.Parse(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Boolean?) && targetType != typeof(Boolean))
            {
                throw new InvalidOperationException("The target must be Nullable<Boolean> or Boolean");
            }
            return value == null ? true : !Boolean.Parse(value.ToString());
        }
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
            {
                throw new InvalidOperationException("The target must be Nullable<Boolean>");
            }
            Boolean isVisible = Boolean.Parse(value.ToString());
            return isVisible ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
